﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using CommonBase;
using MCDomain.DataAccess;
using MCDomain.Model;
using MContracts.Classes;
using MContracts.Controls.Dialogs;
using MContracts.ViewModel;
using MContracts.ViewModel.Helpers;
using UIShared.Common;
using McUIBase.Factories;

namespace MContracts.View
{
    /// <summary>
    /// Interaction logic for ActsView.xaml
    /// </summary>
    public partial class ActsView : UserControl
    {
        public ActsView()
        {
            InitializeComponent();
        }

        private ActsViewModel ViewModal
        {
            get { return DataContext as ActsViewModel; }
        }


        public void RecalcAggregates(object sender, EventArgs e)
        {
            ActsDG.CalculateAggregates();
        }


        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            
            e.DependencyPropertyAction<ActsViewModel>((old, newVal) =>
                {
                    old.Do(x => x.StatusChanged -= RecalcAggregates);
                    old.Do(x => x.RequestShowActDesigner -= Viewmodel_RequestShowActDesigner);
                    newVal.Do(x => x.StatusChanged += RecalcAggregates);
                    newVal.Do(x => x.RequestShowActDesigner += Viewmodel_RequestShowActDesigner);
                });
        }

        private void Viewmodel_RequestShowActDesigner(object sender, EventParameterArgs<Act> e)
        {
            using (ActDesignerViewModel vm = CreateActDesgnerViewModel(e.Parameter))
            {
                var dlg = new ActDesignerDialog {DataContext = vm};
                dlg.ShowDialog();
            }
        }

        private ActDesignerViewModel CreateActDesgnerViewModel(Act act)
        {
            var vm = new ActDesignerViewModel(RepositoryFactory.CreateContractRepository());

            vm.CurrentContract = vm.Repository.GetContractdoc(ViewModal.ContractObject.Id);
            vm.CurrentAct = act == null
                                ? ViewModal.CreateNewAct(vm)
                                : vm.Repository.TryGetContext().Acts.Single(x => x.Id == act.Id);
            vm.CurrentSchedule =
                vm.Repository.TryGetContext().Schedulecontracts.Single(x => x.Id == ViewModal.SelectedSchedule.Id).
                    Schedule;

            return vm;
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ViewModal != null)
            {
                (ViewModal).SendPropertyChanged("StagePriceWithNDSColumnTitle");
                (ViewModal).SendPropertyChanged("StagePriceWithNoNDSColumnTitle");
                (ViewModal).SendPropertyChanged("NDSColumnTitle");
                (ViewModal).SendPropertyChanged("PrepaymentColumnTitle");
                (ViewModal).SendPropertyChanged("TransferColumnTitle");
            }
        }
    }
}